package org.wang.generalcode.support.trans.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.support.GenericConversionService;
import org.wang.generalcode.support.trans.aspect.TranslateAspect;
import org.wang.generalcode.support.trans.core.service.DictTranslateService;
import org.wang.generalcode.support.trans.core.service.SummaryExtractService;
import org.wang.generalcode.support.trans.core.service.WriteFieldNameService;
import org.wang.generalcode.support.trans.service.impl.*;
import org.wang.generalcode.support.trans.service.impl.defaults.DefaultWriteFieldNameServiceImpl;
import org.wang.generalcode.support.trans.service.impl.wrapper.IPageUnWrapper;
import org.wang.generalcode.support.trans.util.TranslatorUtil;

import javax.sql.DataSource;

/**
 * 翻译配置
 *
 * @author nn200433
 * @date 2022-08-18 018 16:37:42
 */
@Configuration
@Import(cn.hutool.extra.spring.SpringUtil.class)
public class TranslatorConfig {

    /**
     * 字典翻译切面
     *
     * @param conversionService 通用转换服务
     * @return {@link TranslateAspect }
     * @author nn200433
     */
    @Bean
    public TranslateAspect translateAspect(GenericConversionService conversionService) {
        return new TranslateAspect(conversionService);
    }

    /**
     * 注册字典翻译服务默认实现
     *
     * @return {@link DictTranslateService }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(DictTranslateService.class)
    public DictTranslateService dictTranslateService() {
        return new DefaultDictTranslateServiceImpl();
    }

    /**
     * 注册摘要提取服务默认实现
     *
     * @return {@link DictTranslateService }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(SummaryExtractService.class)
    public SummaryExtractService summaryExtractService() {
        return new DefaultSummaryExtractServiceImpl();
    }

    /**
     * 注册写入字段服务默认实现
     *
     * @return {@link WriteFieldNameService }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(WriteFieldNameService.class)
    public WriteFieldNameService writeFieldNameService() {
        return new DefaultWriteFieldNameServiceImpl();
    }

    /**
     * 注册IPage解包器
     *
     * @return {@link IPageUnWrapper }<{@link Object }>
     * @author nn200433
     */
    @Bean
    public IPageUnWrapper<Object> iPageUnWrapper() {
        return new IPageUnWrapper<Object>();
    }

    // region =========================================== 翻译服务 ===========================================

    /**
     * 数据库翻译默认实现
     *
     * @param dataSource 数据源
     * @return {@link DataBaseTranslator }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(DataBaseTranslator.class)
    public DataBaseTranslator dataBaseTranslator(DataSource dataSource) {
        return new DataBaseTranslator(dataSource);
    }

    /**
     * 脱敏翻译
     *
     * @return {@link DesensitizedTranslator }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(DesensitizedTranslator.class)
    public DesensitizedTranslator desensitizedTranslator() {
        return new DesensitizedTranslator();
    }

    /**
     * 数据字典翻译
     *
     * @param dictTranslateService 字典翻译服务
     * @return {@link DictCacheTranslator }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(DictCacheTranslator.class)
    public DictCacheTranslator dictCacheTranslator(DictTranslateService dictTranslateService) {
        return new DictCacheTranslator(dictTranslateService);
    }

    /**
     * 枚举翻译
     *
     * @return {@link EnumTranslator }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(EnumTranslator.class)
    public EnumTranslator enumTranslator() {
        return new EnumTranslator();
    }

    /**
     * JSON 翻译转换器
     *
     * @return {@link JsonConvertTranslator }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(JsonConvertTranslator.class)
    public JsonConvertTranslator jsonConvertTranslator() {
        return new JsonConvertTranslator();
    }

    /**
     * 摘要提取翻译
     *
     * @param summaryExtractService 摘要提取服务
     * @return {@link SummaryExtractTranslator }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(SummaryExtractTranslator.class)
    public SummaryExtractTranslator summaryExtractTranslator(SummaryExtractService summaryExtractService) {
        return new SummaryExtractTranslator(summaryExtractService);
    }

    // endregion ===============================================================================================

    /**
     * 翻译工具类
     *
     * @param conversionService 通用转换服务
     * @return {@link TranslatorUtil }
     * @author nn200433
     */
    @Bean
    public TranslatorUtil translatorUtil(GenericConversionService conversionService) {
        return new TranslatorUtil(conversionService);
    }

}
